package com.mengxinya.ys.sdk.tencent;

import com.mengxinya.ys.sdk.ServerDetail;
import com.mengxinya.ys.sdk.ServerManager;
import com.mengxinya.ys.sdk.ServerSdkException;
import com.mengxinya.ys.sdk.ServerState;
import com.tencentcloudapi.common.Credential;
import com.tencentcloudapi.common.exception.TencentCloudSDKException;
import com.tencentcloudapi.common.profile.ClientProfile;
import com.tencentcloudapi.common.profile.HttpProfile;
import com.tencentcloudapi.cvm.v20170312.CvmClient;
import com.tencentcloudapi.cvm.v20170312.models.*;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;

import java.util.Arrays;

/**
 * 腾讯云服务器管理器实现类
 */
@Service
@Slf4j
public class TencentServerManager implements ServerManager {
    private final Credential cred;

    public TencentServerManager(TencentSdkConfig sdkConfig) {

        // 实例化一个认证对象，入参需要传入腾讯云账户 SecretId 和 SecretKey，此处还需注意密钥对的保密
        // 代码泄露可能会导致 SecretId 和 SecretKey 泄露，并威胁账号下所有资源的安全性。以下代码示例仅供参考，建议采用更安全的方式来使用密钥，请参见：https://cloud.tencent.com/document/product/1278/85305
        // 密钥可前往官网控制台 https://console.cloud.tencent.com/cam/capi 进行获取
        this.cred = new Credential(sdkConfig.getSecretId(), sdkConfig.getSecretKey());
    }


    @Override
    public void startServerInstance(ServerDetail detail) throws ServerSdkException {
        try{
            // 实例化一个http选项，可选的，没有特殊需求可以跳过
            HttpProfile httpProfile = new HttpProfile();
            httpProfile.setEndpoint("cvm.tencentcloudapi.com");
            // 实例化一个client选项，可选的，没有特殊需求可以跳过
            ClientProfile clientProfile = new ClientProfile();
            clientProfile.setHttpProfile(httpProfile);
            // 实例化要请求产品的client对象,clientProfile是可选的
            CvmClient client = new CvmClient(cred, "", clientProfile);

            client.setRegion(detail.getRegion());
            // 实例化一个请求对象,每个接口都会对应一个request对象
            StartInstancesRequest req = new StartInstancesRequest();
            req.setInstanceIds(new String[]{detail.getServerId()});

            // 返回的resp是一个StartInstancesResponse的实例，与请求对象对应
            StartInstancesResponse resp = client.StartInstances(req);
            // 输出json格式的字符串回包
            log.info(StopInstancesResponse.toJsonString(resp));
        } catch (TencentCloudSDKException e) {
            throw new ServerSdkException(e.getMessage(), e);
        }
    }

    @Override
    public void stopServerInstance(ServerDetail detail) throws ServerSdkException {
        try{
            // 实例化一个http选项，可选的，没有特殊需求可以跳过
            HttpProfile httpProfile = new HttpProfile();
            httpProfile.setEndpoint("cvm.tencentcloudapi.com");
            // 实例化一个client选项，可选的，没有特殊需求可以跳过
            ClientProfile clientProfile = new ClientProfile();
            clientProfile.setHttpProfile(httpProfile);
            // 实例化要请求产品的client对象,clientProfile是可选的
            CvmClient client = new CvmClient(cred, "", clientProfile);

            client.setRegion(detail.getRegion());
            // 实例化一个请求对象,每个接口都会对应一个request对象
            StopInstancesRequest req = new StopInstancesRequest();

            req.setInstanceIds(new String[]{detail.getServerId()});

            // 返回的resp是一个StopInstancesResponse的实例，与请求对象对应
            StopInstancesResponse resp = client.StopInstances(req);
            // 输出json格式的字符串回包
            log.info(StopInstancesResponse.toJsonString(resp));
        } catch (TencentCloudSDKException e) {
            throw new ServerSdkException(e.getMessage(), e);
        }
    }

    @Override
    public ServerState getServerState(ServerDetail detail) throws ServerSdkException {
        try {
            // 实例化一个http选项，可选的，没有特殊需求可以跳过
            HttpProfile httpProfile = new HttpProfile();
            httpProfile.setEndpoint("cvm.tencentcloudapi.com");
            // 实例化一个client选项，可选的，没有特殊需求可以跳过
            ClientProfile clientProfile = new ClientProfile();
            clientProfile.setHttpProfile(httpProfile);
            // 实例化要请求产品的client对象,clientProfile是可选的
            CvmClient client = new CvmClient(cred, "", clientProfile);

            client.setRegion(detail.getRegion());
            // 实例化一个请求对象,每个接口都会对应一个request对象
            DescribeInstancesStatusRequest req = new DescribeInstancesStatusRequest();

            req.setInstanceIds(new String[]{detail.getServerId()});

            // 返回的resp是一个StopInstancesResponse的实例，与请求对象对应
            DescribeInstancesStatusResponse resp = client.DescribeInstancesStatus(req);

            ServerState serverState = new ServerState();
            InstanceStatus instanceStatus = Arrays.stream(resp.getInstanceStatusSet()).findFirst().orElseThrow();
            serverState.setServerId(instanceStatus.getInstanceId());
            serverState.setServerState(instanceStatus.getInstanceState());

            return serverState;
        } catch (TencentCloudSDKException e) {
            throw new ServerSdkException(e.getMessage(), e);
        }
    }
}
